package com.ymatou.iapi.shoppingcartservice.testcase;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductUpdateDeliveryTypeReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductUpdateDeliveryTypeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ShoppingcartProductUpdateDeliveryType {
	
	private static ShoppingcartProductUpdateDeliveryTypeCall shoppingcartProductUpdateDeliveryTypeCall = new ShoppingcartProductUpdateDeliveryTypeCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品物流信息");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductUpdateDeliveryTypeCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateDeliveryType_001() throws Exception {
		Logger.start(true,"更新商品物流信息");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateDeliveryTypeReq req = new ShoppingcartProductUpdateDeliveryTypeReq();
			
			int deliveryType = 5;
			
			req.setShoppingCartId(shoppingCartId);
			req.setDeliveryType(deliveryType);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductUpdateDeliveryTypeCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId,
							"deliveryType", deliveryType,
							"shoppingCartId", shoppingCartId));
			
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateDeliveryType_002() throws Exception {
		Logger.start(true,"更新传入物流type为0");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateDeliveryTypeReq req = new ShoppingcartProductUpdateDeliveryTypeReq();
			
			int deliveryType = 0;
			
			req.setShoppingCartId(shoppingCartId);
			req.setDeliveryType(deliveryType);
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductUpdateDeliveryTypeCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("deliveryType:错误的deliveryType;", message, "不传入deliveryType提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
